﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Text;
using Newtonsoft.Json;

namespace YiDingCanWp7
{
    public partial class AddShop : PhoneApplicationPage
    {
        string url = BaseConfig.BaseUrl + "/Shop/AddShopForMobile";

        string catsurl = BaseConfig.BaseUrl + "/Shop/ShopCats";
        public AddShop()
        {
            InitializeComponent();
            WebClient webClient = new WebClient();
            webClient.Encoding = Encoding.UTF8;
            webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(webClient_UploadStringCompleted2);
            webClient.UploadStringAsync(new Uri(catsurl, UriKind.RelativeOrAbsolute), "GET");
        }
        private void Btn_Submit_Click(object sender, RoutedEventArgs e)
        {
            performanceProgressBar.IsIndeterminate = true;
			#region 检验是否按照要求输入数据
			if(string.IsNullOrEmpty(textBox_Title.Text.Trim())){
				MessageBox.Show("店铺名称不能为空！");
				return;
			}
			if(string.IsNullOrEmpty( textBox_phone.Text.Trim())){
				MessageBox.Show("店铺电话不能为空！");
				return;
			}
			#endregion
            try
            {
                ShopDetailDTO shop = new ShopDetailDTO();
                shop.title = textBox_Title.Text;
                shop.category = listBox_category.SelectedIndex+1;
                shop.phone = textBox_phone.Text;
                shop.lat = Utils.GetLocationProperty()[0];
                shop.lng = Utils.GetLocationProperty()[1];
                shop.category = ((ShopCatDTO)listBox_category.SelectedItem).catid;
                shop.DeviceUniqueId = PhoneUtils.GetDeviceUniqueId();
                string jsonData = JsonConvert.SerializeObject(shop);
                WebClient webClient = new WebClient();
                webClient.Headers["Content-type"] = "application/json";
                webClient.Encoding = Encoding.UTF8;
                webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(webClient_UploadStringCompleted);
                webClient.UploadStringAsync(new Uri(url, UriKind.RelativeOrAbsolute), "POST", jsonData);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }
        void webClient_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
        {
            MessageDTO message = JsonConvert.DeserializeObject<MessageDTO>(e.Result);
            performanceProgressBar.IsIndeterminate = false;
            MessageBox.Show(message.message);
        }
        void webClient_UploadStringCompleted2(object sender, UploadStringCompletedEventArgs e)
        {
            List<ShopCatDTO> cats = JsonConvert.DeserializeObject<List<ShopCatDTO>>(e.Result);
            listBox_category.ItemsSource = cats;
            listBox_category.SelectedIndex = 2;
        }

        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            performanceProgressBar.IsIndeterminate = true;
            #region 检验是否按照要求输入数据
            if (string.IsNullOrEmpty(textBox_Title.Text.Trim()))
            {
                MessageBox.Show("店铺名称不能为空！");
                return;
            }
            if (string.IsNullOrEmpty(textBox_phone.Text.Trim()))
            {
                MessageBox.Show("店铺电话不能为空！");
                return;
            }
            #endregion
            try
            {
                ShopDetailDTO shop = new ShopDetailDTO();
                shop.title = textBox_Title.Text;
                shop.category = listBox_category.SelectedIndex + 1;
                shop.phone = textBox_phone.Text;
                shop.lat = Utils.GetLocationProperty()[0];
                shop.lng = Utils.GetLocationProperty()[1];
                shop.category = ((ShopCatDTO)listBox_category.SelectedItem).catid;
                shop.DeviceUniqueId = PhoneUtils.GetDeviceUniqueId();
                string jsonData = JsonConvert.SerializeObject(shop);
                WebClient webClient = new WebClient();
                webClient.Headers["Content-type"] = "application/json";
                webClient.Encoding = Encoding.UTF8;
                webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(webClient_UploadStringCompleted);
                webClient.UploadStringAsync(new Uri(url, UriKind.RelativeOrAbsolute), "POST", jsonData);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }
    }
}